473,480 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Event on dynamically created linkbutton in C#

Hi,

My web from has one button and by clicking this button a list of
linkbuttons must be dynamically displayed based on information in
database. Then click any one of these linkbuttons another set of
linkbuttons will be displayed, and so forth... My code works, but I have
to click twice every time to get it running correctly. I can not figure
out what's wrong in my code.

My code:

public class WebForm1 : System.Web.UI.Page
{
static string conn = ConfigurationSettings.AppSettings.Get("ConnStr");
protected SqlConnection mysqlConn = new SqlConnection(conn);
protected System.Web.UI.WebControls.Button button;
protected System.Web.UI.WebControls.Panel panel;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
Session["parentId"] = 0;
else
CreateDynamicButtons();
}

private void CreateDynamicButtons()
{
int parentId = (int)Session["parentId"];
SqlCommand cmd = mysqlConn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select categoryname from mycategory where
parentid=" + parentId;

SqlDataAdapter myAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
myAdapter.SelectCommand = cmd;
myAdapter.Fill(ds, "mycategory");

DataTable dt = new DataTable();
dt = ds.Tables[0];

if(panel.Controls.Count!=0)
panel.Controls.Clear();

Table table = new Table();
for (int i=0; i<dt.Rows.Count; i++)
{
LinkButton lbt = new LinkButton();
TableCell cell = new TableCell();
TableRow row = new TableRow();

lbt.Text = dt.Rows[i].ItemArray[0].ToString();
lbt.Font.Size = FontUnit.Medium;
lbt.CommandName = "CategoryName";
lbt.CommandArgument = lbt.Text;
lbt.Command += new
System.Web.UI.WebControls.CommandEventHandler(lbt_ click);
cell.Controls.Add(lbt);
row.Cells.Add(cell);
table.Rows.Add(row);
}

panel.Controls.Add(table);
panel.Visible = true;
}

private void lbt_click(object sender, CommandEventArgs e)
{
LinkButton lbtSender = (LinkButton)sender;

string query = "Select CategoryId from mycategory where
categoryname='" + lbtSender.CommandArgument + "'";

SqlCommand cmd = new SqlCommand ( query, mysqlConn );
cmd.Connection.Open();
Session["parentId"] = (int)cmd.ExecuteScalar();
cmd.Connection.Close();
CreateDynamicButtons();
}

private void button_Click(object sender, System.EventArgs e)
{
Session["parentId"] = 0;
CreateDynamicButtons();
}
}

Thank you in advance.

Alice

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 12925

Hi Alice
I have seen behavior like this when you are creating more that one instance
of the same control ( calling the new statement " the one that allocate
memory " more than one time). Mostly this is the cause of what you have.
try to check for that by searching for the new keyword . Make sure also
that the new keyword is not called inside a loop block .
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2
Hi,Mohamoss

What does "new keyword" mean?

I guess the problem is somewhere in attaching event handler to
dynamically created linkbuttons between pages postback. But I'm not
sure...

Please give me more detailed info. Thank you!

Alice
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Hi Alice
You saying that the link button is created dynamically make me almost sure
that the problem is what I think it is . You are calling the new
constructor two times. What I mean by the new keyword Is when you say
Object name;
name = new Object(); // you allocate memory to the object
or it could all be done once
Object name = new Object();
For sure you know what this is but may be I was not clear in my previous
post . my advice again is
Make sure that the constructor is not called twice for I have seen many
cases where this was the cause of such problem. The problem is , you have
two objects with the same name , only one of them has the event associated
with the handler

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1443
by: Robert Harty | last post by:
Hello all, I have a strange problem with a HtmlTable control, which is dynamically created based on the results of a query. Each row in the table has a LinkButton added to it. When I create...
2
3692
by: Linda | last post by:
Hi, How do I dynamically add linkbuttons and wire them to same event? I am able to add linkbuttons but they do not fire the event. Does anybody have a working sample? Many thanks, Linda
2
2167
by: jorge | last post by:
Hello I have the following situation: (everything is dynamic (controls.add)) 1. Button.Init { WasButtonClickFired = true } 2. TextBox.TextChanged { WasButtonClickFired?
4
2102
by: The Alchemist | last post by:
I am having a problem with a dynamically-generated Datagrid. It is important to point out that this problem does not exist with a design-time created Datagrid, but only with a dynamically generated...
2
3851
by: hn | last post by:
Hi, I have linkbuttons created dynamically and they display fine on the web page. However, when I click on the those link buttons, the event doesn't fire. Please tell me what's wrong with the...
1
2297
by: geronimi | last post by:
I want to create a linkbutton in a cell because not every row needs one (so I can't setup a linkbuttoncolumn instead of a boundcolumn.) First, i create a linkbutton in the datagrid_ItemDataBound...
5
4692
by: =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post by:
Hi, I have a strange issue occurring with LinkButtons that are dynamically added to each (data) row of my DataGrid on that grid's ItemDataBound event. Each LinkButton is assigned its own event...
9
11872
by: lilOlMe | last post by:
Hi there! I have generated a GridView that looks something like: SportName| CompanyNameX |CompanyNameY |CompanyNameZ Hockey.....| Shipping------------ |Accounting-------- |Shipping------------...
1
1432
by: successgac | last post by:
//I have the event declerations as follows down in a separate class file public event CommandEventHandler PageNumberClicked; //I have created link button dynamically using the following C# code...
8
12932
by: Hamayun Khan | last post by:
Hi I have created linkbuttons dynamically using the below code Sub createlinkbutton(ByVal commandtext As String, ByVal Cmdarg As String, ByVal pane As Panel, ByVal count As Int32) Dim i =...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.